 #include "tiempo.h"
 
 /*
  * Obtains current time value, as seconds since 1970.
  */
time_t getCurrentTime(){
  time_t t;
  time(&t);
  return t ;	    
}

/* 
 * Obtains the time value for today at 00:00h
 */
time_t getToday() {
  struct tm * ltime;		
  time_t t1;
  time(&t1);
  ltime = localtime (&t1);
  t1 -= (ltime->tm_hour*3600 + ltime->tm_min*60 + ltime->tm_sec);
  return t1;
}

/*
 * Gets the time value of "days" days before today at 00:00h
 * Returns:
 *    time value corresponding to today at 00:00h if days ==0 --> the same as getToday()
 *    time value corresponding to yesterday at 00:00h if days ==1
 *    time value corresponding to x days before at 00:00h if days ==x
 */
time_t getStartTimeDaysBefore(int days) {
  time_t t;
  t = getToday();
  t -= (days * SECONDS_IN_A_DAY);
  return t;
}
	
/*
 * Calls to ctime, but skipping the last '\n' char.
 * Therefore it just returs a formatted char * value corresponding
 * to parameter t.
 * Note, a call to free is mandatory after using the returned value.
 */
char *getTimeAsString(time_t t) {
  char buff[26];
  static char str[25];
  strcpy(str,"");
  
  ctime_r (&t, buff);
  
  strncpy (str,buff,24);
  str[24] = '\0';
  return(str);			
}

